package dao

import (
	"fmt"
	"trpg-common/constants"
	"trpg-common/utils/gorm_util"
	"trpg-common/utils/json_util"
	"trpg-message/entity"
)

type RoomDao struct{}

func (RoomDao) GetGreatSuccessAndGreatFail(roomId int64) (int32, int32) {
	room := entity.Room{}
	room.Id = roomId
	room.IsDeleted = constants.DeleteNo
	gorm_util.SelectOneByCondition(&room)
	return room.GreatSuccess, room.GreatFail
}

func (RoomDao) SetGreatFail(roomId int64, greatFail int32) {
	room := entity.Room{}
	room.Id = roomId
	room.GreatFail = greatFail
	gorm_util.UpdateOneByCondition(&room)
}

func (RoomDao) SetGreatSuccess(roomId int64, greatSuccess int32) {
	room := entity.Room{}
	room.Id = roomId
	room.GreatSuccess = greatSuccess
	gorm_util.UpdateOneByCondition(&room)
}

func (RoomDao) GetRoomBindCharacter(roomId int64, uid string) map[string]int32 {
	roomUser := entity.RoomUser{}
	roomUser.RoomId = roomId
	roomUser.Uid = uid
	roomUser.IsDeleted = constants.DeleteNo
	gorm_util.SelectOneByCondition(&roomUser)
	character := entity.Character{}
	character.Id = roomUser.CharacterId
	character.IsDeleted = constants.DeleteNo
	gorm_util.SelectOneByCondition(&character)
	if character.RememberedVars == "" {
		return nil
	} else {
		m := make(map[string]int32)
		json_util.UnMarshal([]byte(character.RememberedVars), &m)
		return m
	}
}

func (RoomDao) SetCharacterAttr(roomId int64, uid string, attr map[string]int32) {
	roomUser := entity.RoomUser{}
	roomUser.RoomId = roomId
	roomUser.Uid = uid
	roomUser.IsDeleted = constants.DeleteNo
	gorm_util.SelectOneByCondition(&roomUser)
	if roomUser.CharacterId == 0 {
		return
	}
	character := entity.Character{}
	character.Id = roomUser.CharacterId
	character.IsDeleted = constants.DeleteNo
	gorm_util.SelectOneByCondition(&character)
	character.RememberedVars = string(json_util.Marshal(attr))
	attrStr := ""
	for k, v := range attr {
		attrStr += fmt.Sprintf("%s%d", k, v)
	}
	character.RememberedVarsStr = attrStr
	gorm_util.InsertUpdateOne(&character)
}
